﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;

namespace SEO.Spider
{
    /// <summary>
    /// 版权所有: 版权所有(C) 2013，上海云里网络科技有限公司
    /// 内容摘要: 百度知道搜索结果采集类
    /// 完成日期：2013年2月19日
    /// 版    本：V1.0 
    /// 作    者：Adin
    /// </summary>
    public class ESBaiduZhidao
    {

        /// <summary>
        /// 根据规则抓取新闻列表页面内容
        /// </summary>
        /// <param name="strWebUrl">网址</param>
        /// <param name="strStartMarkString">开始标记</param>
        /// <param name="strEndMarkString">结束标记</param>
        /// <param name="rb">显示实时消息控件</param>
        /// <param name="strMainChannelID">文章主分类ID</param>
        /// <param name="strChannelID">文章子分类ID</param>
        /// <param name="strWebName">网站频道名称</param>
        /// <param name="iCatchID">抓取地址ID</param>
        /// <param name="strWebCharset">网页编码</param>
        /// <param name="bAPISoft">云里邮件营销大师目录接口</param>
        public static void AccordingURLGetNewsList(string strWebUrl, string strStartMarkString, string strEndMarkString, System.Windows.Forms.RichTextBox rb, string strMainChannelID, string strChannelID, string strWebName, int iCatchID,string strWebCharset,bool bAPISoft)
        {
            try
            {
                SEO.Utility.WebClient wc = new SEO.Utility.WebClient();
                wc.Encoding = System.Text.Encoding.GetEncoding(strWebCharset);
                wc.OpenRead(strWebUrl);
                string strGetHtmlCode = wc.RespHtml;

                //匹配列表源代码
                Regex rx = new Regex(strStartMarkString + "(?<content>[\\s\\S]*)?" + strEndMarkString, RegexOptions.Singleline);
                string strGetListHtmlContent = rx.Match(strGetHtmlCode).Result("${content}");

                strGetListHtmlContent = strGetListHtmlContent.Replace("|","");
                strGetListHtmlContent = strGetListHtmlContent.Replace("<dl class=\"result-item\">", "|");
                string[] splitContent = strGetListHtmlContent.Split(new char[] {'|'});
                for (int i = 0; i < splitContent.Length;i++ )
                {
                    try
                    {
                        string strEmail = splitContent[i];
                        strEmail = Regex.Match(strEmail, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*").ToString();

                        if (!string.IsNullOrEmpty(strEmail))
                        {
                            SEO.DAL.ESEmailsDAL emaildal = new DAL.ESEmailsDAL();
                            if (!emaildal.CheckEmailAddressIsExist(strEmail))
                            {
                                //替换掉匹配到的email中包含中文字符
                                strEmail = System.Text.RegularExpressions.Regex.Replace(strEmail, @"[\u4e00-\u9fa5]+", "");
                                //替换掉日文字符
                                strEmail = System.Text.RegularExpressions.Regex.Replace(strEmail, @"[\u0800-\u4e00]+", "");
                                //替换掉韩文字符
                                strEmail = System.Text.RegularExpressions.Regex.Replace(strEmail, @"[\uac00-\ud7ff]+", "");

                                SEO.Utility.PublicValue.SaveMessage(Convert.ToDateTime(DateTime.Now).ToString("MM-dd HH:mm:ss") + " 已采集到Email -> " + strEmail);
                                //rb.Text = SEO.Utility.PublicValue.GetMessage();

                                if (strEmail.Contains("@"))
                                {
                                    string[] splitAt = strEmail.Split(new char[] { '@' });
                                    string strStartAt = splitAt[0];
                                    if (!string.IsNullOrEmpty(strStartAt))
                                    {
                                        ESEmails.InsertEmails(strEmail, strWebName, strWebUrl, bAPISoft, strChannelID); ;
                                        //iNum = 2;//匹配成功
                                    }
                                }
                            }
                            else
                            {
                                //iNum = 3;//电子邮件重复
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        SEO.Utility.Log.SaveErrorLog("匹配电子邮件失败,错误信息：" + ex.Message);
                    }
                }

                //GetNewsListAllLink(strGetListHtmlContent, rb, strMainChannelID, strChannelID, strWebName, strWebCharset);
            }
            catch (Exception ex)
            {
                string strError = "文章列表抓取匹配失败，主分类ID：" + strMainChannelID + "，子分类ID：" + strChannelID + "\r\n" + ex.Message;

                SEO.Utility.PublicValue.SaveMessage(Convert.ToDateTime(DateTime.Now).ToString("MM-dd HH:mm:ss") + " 【" + strWebName + "】列表地址抓取失败！");
                rb.Text = SEO.Utility.PublicValue.GetMessage();
            }

        }

      }
}
